*******************************************************************************
*set working directory
global folder ".."


cd $folder\temp


**********************************
// fed announcement: flows
**********************************
use "$folder\data\daily_flow_assets", clear 
keep  if InvestmentType=="Open-End Fund"
encode(SecId), g(SecIdn) 
drop SecId

merge m:1 date using "$folder\data\covid_confirmed"
replace confirmed_gr=0 if confirmed_gr==.
replace confirmed_gr=0 if confirmed<=100
drop if _m==2
drop _m

g ln_size = log(assets)

g outflow =-flow_pct // change to outflows

sum outflow, d
winsor2 outflow, cut(1 99) replace  

sort SecId date
bysort SecId: g return=(nav/nav[_n-1]-1)*100
sum return, d
winsor2 return, cut(1 99) replace  
 
g crisis=(date>=td(1march2020)&date<=td(15march2020))


g march15=(date>=td(16march2020)&date<=td(16march2020))
g march23=(date>=td(23march2020)&date<=td(23march2020))
g april09=(date>=td(09april2020)&date<=td(09april2020))

g march15_3day=(date>=td(16march2020)&date<=td(18march2020))
g march23_3day=(date>=td(23march2020)&date<=td(25march2020))
g april09_3day=(date>=td(09april2020)&date<=td(14april2020))

g march15_5day=(date>=td(16march2020)&date<=td(20march2020))
g march23_5day=(date>=td(23march2020)&date<=td(27march2020))
g april09_5day=(date>=td(09april2020)&date<=td(16april2020))



sort SecId date
bysort Sec: g L_outflow=outflow[_n-1]
sort SecId date
bysort Sec: g L_return=return[_n-1]
bysort SecId: g ab_outflow=outflow-1/5*(outflow[_n-1]+outflow[_n-2]+outflow[_n-3]+outflow[_n-4]+outflow[_n-5])
bysort SecId: g ab_outflow2=outflow-1/7*(outflow[_n-1]+outflow[_n-2]+outflow[_n-3]+outflow[_n-4]+outflow[_n-5]+outflow[_n-6]+outflow[_n-7])
bysort SecId: g ab_outflow3=outflow-1/1*(outflow[_n-1])

label var crisis "Covid-19 crisis"
label var march15 "Treasury bond purchase"
label var march23 "Corp bond purchase"
label var april09 "Corp bond purchase extension"
label var march15_3day "Treasury bond purchase"
label var march23_3day "Corp bond purchase"
label var april09_3day "Corp bond purchase extension"
label var march15_5day "Treasury bond purchase"
label var march23_5day "Corp bond purchase"
label var april09_5day "Corp bond purchase extension"
label var L_outflow "Lag outflows"
label var confirmed_gr "Infection growth"
label var outflow "Outflows"

g month=mofd(date)

tsset SecId date



estpost summarize outflow  if date>=td(1march2020)&date<=td(31march2020) ///
 , d
est store sum_stat 
esttab sum_stat using "$folder\output\summary_stats_daily_outflow.tex", replace ///
cells("mean(fmt(%9.3f)) sd(fmt(%9.3f))  p10(fmt(%9.3f)) p25(fmt(%9.3f)) p50(fmt(%9.3f)) p75(fmt(%9.3f)) p90(fmt(%9.3f)) ") ///
label booktabs nonum gaps noobs ///
collabels( "mean" "sd" "p10" "p25" "p50" "p75" "p90") width(\hsize) 


set more off
eststo clear
local contr  
local y outflow 
local x  march15 march23 april09 
local cluster SecId date
local fixedeffect month SecId
local contr  L_outflow confirmed_gr L_return
eststo:reghdfe `y'  `x' `contr'  if Type=="Government", ab(`fixedeffect')  cluster(`cluster')
estadd local FE2 "Yes"
eststo:reghdfe `y'  `x' `contr'  if Type=="IG", ab(`fixedeffect')  cluster(`cluster')
estadd local FE2 "Yes"
eststo:reghdfe `y'  `x' `contr'  if Type=="HY", ab(`fixedeffect')  cluster(`cluster')
estadd local FE2 "Yes"
eststo:reghdfe `y'  `x' `contr'  if Type=="BL", ab(`fixedeffect')  cluster(`cluster')
estadd local FE2 "Yes"

esttab 
esttab using  "$folder\output\fed_annoucement_flow_by_obj.tex", ///
replace b(3) se(3) star(* 0.10 ** 0.05 *** 0.01  )   ///
mtitles(  "Govt" "IG" "HY" "BL")   ///
ar2  br  label nocon  nonotes  s(FE2 FE2 FE2 N r2_a, ///
label("Control"  "Fund fixed effects"  "Month fixed effects"  "Observations" "Adj. R-squared") ///
fmt(0 0 0 %9.0fc 3 )) width(\hsize) drop(confirmed_gr L_outflow L_return)


set more off
eststo clear
local contr  
local y return
local x  march15 march23 april09 
local cluster SecId date
local fixedeffect month SecId
local contr  L_outflow confirmed_gr L_return
eststo:reghdfe `y'  `x' `contr'  if Type=="Government", ab(`fixedeffect')  cluster(`cluster')
estadd local FE2 "Yes"
eststo:reghdfe `y'  `x' `contr'  if Type=="IG", ab(`fixedeffect')  cluster(`cluster')
estadd local FE2 "Yes"
eststo:reghdfe `y'  `x' `contr'  if Type=="HY", ab(`fixedeffect')  cluster(`cluster')
estadd local FE2 "Yes"
eststo:reghdfe `y'  `x' `contr'  if Type=="BL", ab(`fixedeffect')  cluster(`cluster')
estadd local FE2 "Yes"

esttab 
esttab using  "$folder\output\fed_annoucement_nav_by_obj.tex", ///
replace b(3) se(3) star(* 0.10 ** 0.05 *** 0.01  )   ///
mtitles(  "Govt" "IG" "HY" "BL"  )   ///
ar2  br  label nocon  nonotes  s(FE2 FE2 FE2 N r2_a, ///
label("Control"  "Fund fixed effects"  "Month fixed effects"  "Observations" "Adj. R-squared") ///
fmt(0 0 0 %9.0fc 3 )) width(\hsize) drop(confirmed_gr L_outflow L_return)



* three day window
set more off
eststo clear
local contr  
local y outflow 
local x  march15_3day march23_3day april09_3day 
local cluster SecId date
local fixedeffect month SecId
local contr  L_outflow confirmed_gr L_return
eststo:reghdfe `y'  `x' `contr'  if Type=="Government", ab(`fixedeffect')  cluster(`cluster')
estadd local FE2 "Yes"
eststo:reghdfe `y'  `x' `contr'  if Type=="IG", ab(`fixedeffect')  cluster(`cluster')
estadd local FE2 "Yes"
eststo:reghdfe `y'  `x' `contr'  if Type=="HY", ab(`fixedeffect')  cluster(`cluster')
estadd local FE2 "Yes"
eststo:reghdfe `y'  `x' `contr'  if Type=="BL", ab(`fixedeffect')  cluster(`cluster')
estadd local FE2 "Yes"

esttab 
esttab using  "$folder\output\fed_annoucement_flow_by_obj_3day.tex", ///
replace b(3) se(3) star(* 0.10 ** 0.05 *** 0.01  )   ///
mtitles(  "Govt" "IG" "HY" "BL")   ///
ar2  br  label nocon  nonotes  s(FE2 FE2 FE2 N r2_a, ///
label("Control"  "Fund fixed effects"  "Month fixed effects"  "Observations" "Adj. R-squared") ///
fmt(0 0 0 %9.0fc 3 )) width(\hsize) drop(confirmed_gr L_outflow L_return)



* one day window with abnormal flows
set more off
eststo clear
local contr  
local y ab_outflow 
local x  march15 march23 april09 
local cluster SecId date
local fixedeffect month SecId
local contr  L_outflow confirmed_gr L_return
eststo:reghdfe `y'  `x' `contr'  if Type=="Government", ab(`fixedeffect')  cluster(`cluster')
estadd local FE2 "Yes"
eststo:reghdfe `y'  `x' `contr'  if Type=="IG", ab(`fixedeffect')  cluster(`cluster')
estadd local FE2 "Yes"
eststo:reghdfe `y'  `x' `contr'  if Type=="HY", ab(`fixedeffect')  cluster(`cluster')
estadd local FE2 "Yes"
eststo:reghdfe `y'  `x' `contr'  if Type=="BL", ab(`fixedeffect')  cluster(`cluster')
estadd local FE2 "Yes"

esttab 
esttab using  "$folder\output\fed_annoucement_flow_by_obj_1day_abnormal_flow.tex", ///
replace b(3) se(3) star(* 0.10 ** 0.05 *** 0.01  )   ///
mtitles(  "Govt" "IG" "HY" "BL")   ///
ar2  br  label nocon  nonotes  s(FE2 FE2 FE2 N r2_a, ///
label("Control"  "Fund fixed effects"  "Month fixed effects"  "Observations" "Adj. R-squared") ///
fmt(0 0 0 %9.0fc 3 )) width(\hsize) drop(confirmed_gr L_outflow L_return)


* three day window with abnormal flows

set more off
eststo clear
local contr  
local y ab_outflow
local x  march15_3day march23_3day april09_3day 
local cluster SecId date
local fixedeffect month SecId
local contr   confirmed_gr L_return
eststo:reghdfe `y'  `x' `contr'  if Type=="Government", ab(`fixedeffect')  cluster(`cluster')
estadd local FE2 "Yes"
eststo:reghdfe `y'  `x' `contr'  if Type=="IG", ab(`fixedeffect')  cluster(`cluster')
estadd local FE2 "Yes"
eststo:reghdfe `y'  `x' `contr'  if Type=="HY", ab(`fixedeffect')  cluster(`cluster')
estadd local FE2 "Yes"
eststo:reghdfe `y'  `x' `contr'  if Type=="BL", ab(`fixedeffect')  cluster(`cluster')
estadd local FE2 "Yes"

esttab 
esttab using  "$folder\output\fed_annoucement_flow_by_obj_3day_abnormal_flow.tex", ///
replace b(3) se(3) star(* 0.10 ** 0.05 *** 0.01  )   ///
mtitles(  "Govt" "IG" "HY" "BL")   ///
ar2  br  label nocon  nonotes  s(FE2 FE2 FE2 N r2_a, ///
label("Control"  "Fund fixed effects"  "Month fixed effects"  "Observations" "Adj. R-squared") ///
fmt(0 0 0 %9.0fc 3 )) width(\hsize) drop(confirmed_gr  L_return)



* five day window with abnormal flows
set more off
eststo clear
local contr  
local y ab_outflow
local x  march15_5day march23_5day april09_5day 
local cluster SecId date
local fixedeffect month SecId
local contr   confirmed_gr L_return
eststo:reghdfe `y'  `x' `contr'  if Type=="Government", ab(`fixedeffect')  cluster(`cluster')
estadd local FE2 "Yes"
eststo:reghdfe `y'  `x' `contr'  if Type=="IG", ab(`fixedeffect')  cluster(`cluster')
estadd local FE2 "Yes"
eststo:reghdfe `y'  `x' `contr'  if Type=="HY", ab(`fixedeffect')  cluster(`cluster')
estadd local FE2 "Yes"
eststo:reghdfe `y'  `x' `contr'  if Type=="BL", ab(`fixedeffect')  cluster(`cluster')
estadd local FE2 "Yes"

esttab 
esttab using  "$folder\output\fed_annoucement_flow_by_obj_5day_abnormal_flow.tex", ///
replace b(3) se(3) star(* 0.10 ** 0.05 *** 0.01  )   ///
mtitles(  "Govt" "IG" "HY" "BL")   ///
ar2  br  label nocon  nonotes  s(FE2 FE2 FE2 N r2_a, ///
label("Control"  "Fund fixed effects"  "Month fixed effects"  "Observations" "Adj. R-squared") ///
fmt(0 0 0 %9.0fc 3 )) width(\hsize) drop(confirmed_gr  L_return)


g assets_billion = assets/10^9
label var  assets_billion "Assets"
label var  return "NAV (daily chg)"
label var  outflow "Outflows"

//Summary stats 

estpost summarize outflow return  assets_billion confirmed_gr,  d
est store sum_stat
esttab sum_stat using "$folder\output\summary_stats_morningstar.tex", replace ///
cells("mean(fmt(%9.3f)) sd(fmt(%9.3f))  p10(fmt(%9.3f)) p25(fmt(%9.3f)) p50(fmt(%9.3f)) p75(fmt(%9.3f)) p90(fmt(%9.3f)) ") ///
label booktabs nonum gaps noobs ///
collabels( "mean" "sd" "p10" "p25" "p50" "p75" "p90") width(\hsize) 


**********************************
// fed announcement: fund-etf analysis
**********************************
use "$folder\data\daily_flow_assets", clear 
merge m:1 crsp_fundno  using "etf_twin"
keep if _m==3
drop _m
bysort crsp_p date: g N=_N
tab N // daily data does not contain etfs

*******************************************************************************
*graph 
*******************************************************************************
// daily flow by category: all
use "$folder\data\daily_flow_assets", clear 
keep if date>=td(01feb2020) & date<=td(30apr2020)
collapse (mean) flow_pct [w=assets] , by(Type date)
encode Type, g(id)
tsset id date
sort id date
bysort id: g cum_flow = sum(flow)

twoway (tsline cum_flow if  Type=="Government", yaxis(1))   ///
(tsline cum_flow if  Type=="IG", yaxis(1)) ///
(tsline cum_flow if  Type=="HY", yaxis(1)) ///
(tsline cum_flow if  Type=="BL", yaxis(1)) ///
, graphregion(color(white)) plotregion(color(white)) bgcolor(none) ///
	 legend(label(1 "Government")  label(2 "Investment-grade")  label(3 "High-yield") label(4 "Loan")  ) ///
	ytitle("Cumulative fund flows (% AUM)") xtitle("") ///	
	ylabel(, nogrid) xlabel(,format(%tdmCY)) tline(15mar2020, lpattern(dot))  tline(23mar2020, lpatter(dot)) tline(9apr2020, lpatter(dot)) 
*graph export  "$folder\output\daily_flow_all.pdf", replace
graph export  "$folder\output\daily_flow_fund.pdf", replace


// daily flow by category: open_end
use "daily_flow_assets", clear   
keep if date>=td(01feb2020) & date<=td(30apr2020)
keep if InvestmentType=="Open-End Fund"
collapse (mean) flow_pct [w=assets] , by(Type date)
encode Type, g(id)
tsset id date
sort id date
bysort id: g cum_flow = sum(flow)

twoway (tsline cum_flow if  Type=="Government", yaxis(1))   ///
(tsline cum_flow if  Type=="IG", yaxis(1)) ///
(tsline cum_flow if  Type=="HY", yaxis(1)) ///
(tsline cum_flow if  Type=="BL", yaxis(1)) ///
, graphregion(color(white)) plotregion(color(white)) bgcolor(none) ///
	 legend(label(1 "Government")  label(2 "Investment grade")  label(3 "High yield") label(4 "Loan")  ) ///
	ytitle("Cummulative fund flows (% AUM)") xtitle("") ///	
	ylabel(, nogrid) xlabel(,format(%tdmCY)) tline(15mar2020, lpattern(dot))  tline(23mar2020, lpatter(dot)) tline(9apr2020, lpatter(dot)) 
graph export  "$folder\output\daily_flow_open_old_type.pdf", replace

// daily flow by category: open_end
use "daily_flow_assets", clear   
keep if InvestmentType=="Open-End Fund"
collapse (mean) flow_pct [w=assets] , by(newtype date)
encode newtype, g(id)
tsset id date
sort id date
bysort id: g cum_flow = sum(flow)

twoway (tsline cum_flow if  newtype=="Government", yaxis(1))   ///
(tsline cum_flow if  newtype=="IG", yaxis(1)) ///
(tsline cum_flow if  newtype=="HY", yaxis(1)) ///
(tsline cum_flow if  newtype=="BL", yaxis(1)) ///
, graphregion(color(white)) plotregion(color(white)) bgcolor(none) ///
	 legend(label(1 "Government")  label(2 "Investment grade")  label(3 "High yield") label(4 "Loan")  ) ///
	ytitle("Cummulative fund flows (% AUM)") xtitle("") ///	
	ylabel(, nogrid) xlabel(,format(%tdmCY)) tline(15mar2020, lpattern(dot))  tline(23mar2020, lpatter(dot)) tline(9apr2020, lpatter(dot)) 
graph export  "$folder\output\daily_flow_open.pdf", replace


// daily flow by category: open_end
use "$folder\data\daily_flow_assets", clear 
keep if InvestmentType=="Open-End Fund"
collapse (sum) assets , by(newtype date)
keep if date==td(28feb2020)

// daily flow by category: etf
use "$folder\data\daily_flow_assets", clear 
keep if InvestmentType=="Exchange-Traded Fund"
collapse (mean) flow_pct [w=assets] , by(newtype date)
encode newtype, g(id)
tsset id date
sort id date
bysort id: g cum_flow = sum(flow)

twoway (tsline cum_flow if  newtype=="Government", yaxis(1))   ///
(tsline cum_flow if  newtype=="IG", yaxis(1)) ///
(tsline cum_flow if  newtype=="HY", yaxis(1)) ///
(tsline cum_flow if  newtype=="BL", yaxis(1)) ///
, graphregion(color(white)) plotregion(color(white)) bgcolor(none) ///
	 legend(label(1 "Government")  label(2 "Investment grade")  label(3 "High yield") label(4 "Loan")  ) ///
	ytitle("Cummulative fund flows (% AUM)") xtitle("") ///	
	ylabel(, nogrid) xlabel(,format(%tdmCY)) tline(15mar2020, lpattern(dot))  tline(23mar2020, lpatter(dot)) tline(9apr2020, lpatter(dot)) 
graph export  "$folder\output\daily_flow_etf.pdf", replace

// daily flow by category: index
use "$folder\data\daily_flow_assets", clear 
keep if InvestmentType=="Open-End Fund" & index_fund_flag!=""
collapse (mean) flow_pct [w=assets] , by(newtype date)
encode newtype, g(id)
tsset id date
sort id date
bysort id: g cum_flow = sum(flow)

twoway (tsline cum_flow if  newtype=="Government", yaxis(1))   ///
(tsline cum_flow if  newtype=="IG", yaxis(1)) ///
(tsline cum_flow if  newtype=="HY", yaxis(1)) ///
(tsline cum_flow if  newtype=="BL", yaxis(1)) ///
, graphregion(color(white)) plotregion(color(white)) bgcolor(none) ///
	 legend(label(1 "Government")  label(2 "Investment grade")  label(3 "High yield") label(4 "Loan")  ) ///
	ytitle("Cummulative fund flows (% AUM)") xtitle("") ///	
	ylabel(, nogrid) xlabel(,format(%tdmCY)) tline(15mar2020, lpattern(dot))  tline(23mar2020, lpatter(dot)) tline(9apr2020, lpatter(dot)) 
graph export  "$folder\output\daily_flow_open_index.pdf", replace



// daily flow by category: active
use "$folder\data\daily_flow_assets", clear 
keep if InvestmentType=="Open-End Fund" & index_fund_flag==""
collapse (mean) flow_pct [w=assets] , by(newtype date)
encode newtype, g(id)
tsset id date
sort id date
bysort id: g cum_flow = sum(flow)

twoway (tsline cum_flow if  newtype=="Government", yaxis(1))   ///
(tsline cum_flow if  newtype=="IG", yaxis(1)) ///
(tsline cum_flow if  newtype=="HY", yaxis(1)) ///
(tsline cum_flow if  newtype=="BL", yaxis(1)) ///
, graphregion(color(white)) plotregion(color(white)) bgcolor(none) ///
	 legend(label(1 "Government")  label(2 "Investment grade")  label(3 "High yield") label(4 "Loan")  ) ///
	ytitle("Cummulative fund flows (% AUM)") xtitle("") ///	
	ylabel(, nogrid) xlabel(,format(%tdmCY)) tline(15mar2020, lpattern(dot))  tline(23mar2020, lpatter(dot)) tline(9apr2020, lpatter(dot)) 
graph export  "$folder\output\daily_flow_open_active.pdf", replace







 
